﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Dao;
using Core.Entity;
using System.Data;

namespace Core.Business
{
    public class TicketBus
    {
        private TicketDao dao;
        public TicketBus() {
            dao = new TicketDao();
        }

        public bool insert(Ticket ticket) {
            return dao.insert(ticket);
        }

        public bool update(Ticket ticket) {
            return dao.update(ticket);
        }

        public bool delete(int id) {
            return dao.delete(id);
        }

        public bool delete(String ids) {
            return dao.delete(ids);
        }

        public Ticket get(int id) {
            DataTable dt = dao.get(id);
            Ticket ticket = new Ticket();

            if (dt.Rows.Count > 0)
            {
                ticket.Id = Convert.ToInt32(dt.Rows[0]["TKT_ID"]);
                ticket.ServiceTypeId = Convert.ToInt32(dt.Rows[0]["SVT_ID"]);
                ticket.DepartmentId = Convert.ToInt32(dt.Rows[0]["DEP_ID"]);
                ticket.OrderLineId = Convert.ToInt32(dt.Rows[0]["OLN_ID"]);
                ticket.TicketCategoryId = Convert.ToInt32(dt.Rows[0]["TKC_ID"]);
                ticket.StaffId = Convert.ToInt32(dt.Rows[0]["STF_ID"]);
                ticket.TicketStatusId = Convert.ToInt32(dt.Rows[0]["TKS_ID"]);
                ticket.Number = dt.Rows[0]["TKT_NUMBER"].ToString();
                ticket.Subject = dt.Rows[0]["TKT_SUBJECT"].ToString();
                ticket.ServicePassword = dt.Rows[0]["TKT_SERVICE_PASSWORD"].ToString();
                ticket.Content = dt.Rows[0]["TKT_CONTENT"].ToString();
                //ticket.TimeStamp = Convert.ToDateTime(dt.Rows[0]["TKT_TIMESTAMP"]);
                ticket.Attachment = dt.Rows[0]["TKT_ATTACHMENT"].ToString();
                ticket.ParentId = Convert.ToInt32(dt.Rows[0]["PARENT_ID"]);
                ticket.ParentType = dt.Rows[0]["PARENT_TYPE"].ToString();

            }
            return ticket;
        }

        public DataTable getByStatus(int statusId, string orderBy) {
            return dao.getByStatus(statusId, orderBy);
        }

        public DataTable getByStaff(int staffId, string statusIds, string orderBy) {
            return dao.getByStaff(staffId, statusIds, orderBy);
        }

        public DataTable getAll(string orderBy) {
            return dao.getAll(orderBy);
        }

        public String issueNumber()
        {
            String pattern = "000000";
            object curNumber = dao.getMaxNumber();
            int next;
            try
            {
                int num = Convert.ToInt32(curNumber);
                next = num + 1;
            }
            catch (Exception ex) 
            {
                next = 1;
            };

            String nextNumber = pattern.Substring(0, pattern.Length - next.ToString().Length) + next;
            return nextNumber;
        }

        public DataTable filter(String keyword, String statuses, String searchIn, bool in2Months, string orderBy)
        {
            return dao.filter(keyword, statuses, searchIn, in2Months, orderBy);
        }
    }
}
